﻿using System;
using System.IO;
using System.Xml;

namespace GameListEditor
{
    ///-------------------------------------------------------------------------------------------------
    /// <summary> SettingsStorage class. </summary>
    ///-------------------------------------------------------------------------------------------------
    class SettingStorage
    {
        private const String SNAPSHOT_SETTINGS_NODE = "snapshotsettings";
        private const String DIRECTORY_ATTRIBUTE = "directory";
        private const String VISIBLE_ATTRIBUTE = "visible";
        private const String AUTOSIZE_ATTRIBUTE = "autosize";

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Directory where snapshots should be loaded from. </summary>
        /// <value> The pathname of the snap shot directory. </value>
        ///-------------------------------------------------------------------------------------------------
        public String SnapShotDirectory
        {
            get;
            set;
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> True if snap shots should be visible, otherwise false. </summary>
        /// <value> true if show snap shots, false if not. </value>
        ///-------------------------------------------------------------------------------------------------
        public bool ShowSnapShots
        {
            get;
            set;
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> True if snap shot panel should autosize to the current snapshot. </summary>
        /// <value> true if automatic size snap shot panel, false if not. </value>
        ///-------------------------------------------------------------------------------------------------
        public bool AutoSizeSnapShotPanel
        {
            get;
            set;
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Default constructor. </summary>
        ///-------------------------------------------------------------------------------------------------
        public SettingStorage()
        {
            Clear(); 
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Save settings. </summary>
        /// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are null. </exception>
        /// <param name="filePath"> Path to settings file. </param>
        ///-------------------------------------------------------------------------------------------------
        public void Save(String filePath)
        {
            if (filePath == null)
                throw new ArgumentNullException("filePath");

            XmlDocument doc = new XmlDocument();

            XmlNode node = doc.CreateElement(SNAPSHOT_SETTINGS_NODE);
            doc.AppendChild(node);

            XmlAttribute attr = doc.CreateAttribute(DIRECTORY_ATTRIBUTE);
            attr.Value = String.IsNullOrWhiteSpace(SnapShotDirectory) ? "" : SnapShotDirectory;
            node.Attributes.Append(attr);

            attr = doc.CreateAttribute(VISIBLE_ATTRIBUTE);
            attr.Value = ShowSnapShots.ToString();
            node.Attributes.Append(attr);

            attr = doc.CreateAttribute(AUTOSIZE_ATTRIBUTE);
            attr.Value = AutoSizeSnapShotPanel.ToString();
            node.Attributes.Append(attr);

            doc.Save(filePath);
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Load settings. </summary>
        /// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are null. </exception>
        /// <exception cref="FileNotFoundException"> Thrown when the requested file is not present. </exception>
        /// <param name="filePath"> Path to settings file. </param>
        ///-------------------------------------------------------------------------------------------------
        public void Load(String filePath)
        {
            if (filePath == null)
                throw new ArgumentNullException("filePath");

            if (!File.Exists(filePath))
                throw new FileNotFoundException("Unable to locate settings file.", filePath);

            XmlDocument doc = new XmlDocument();
            doc.Load(filePath);

            Clear();

            XmlNode node = doc.SelectSingleNode(SNAPSHOT_SETTINGS_NODE);
            if (node != null)
            {
                if (node.Attributes[DIRECTORY_ATTRIBUTE] != null)
                    SnapShotDirectory = node.Attributes[DIRECTORY_ATTRIBUTE].Value;

                if (node.Attributes[VISIBLE_ATTRIBUTE] != null)
                    ShowSnapShots = Convert.ToBoolean(node.Attributes[VISIBLE_ATTRIBUTE].Value);

                if (node.Attributes[AUTOSIZE_ATTRIBUTE] != null)
                    AutoSizeSnapShotPanel = Convert.ToBoolean(node.Attributes[AUTOSIZE_ATTRIBUTE].Value);
            }
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Clear. </summary>
        ///-------------------------------------------------------------------------------------------------
        public void Clear()
        {
            SnapShotDirectory = "";
            ShowSnapShots = false;
            AutoSizeSnapShotPanel = true;
        }
    }
}
